<?
require_once("pg_mon/class_HostCluster.php");

$sql=new PgMonSQL;

$sql->select_c("SELECT id FROM host_cluster WHERE alive ORDER BY hostname");
$hosts=$sql->get_result();

$sql->select_c("INSERT INTO log_time (actual_time,hour_truncate) VALUES (LOCALTIMESTAMP,date_trunc('hour',LOCALTIMESTAMP)) RETURNING id");
$time_res=$sql->get_row_hash();

$time_id=$time_res['id'];

foreach ($hosts as $h_c) {
    $hc=new HostCluster($h_c['id']);
    $hc->populate_bg_stat($time_id);

    $db_stat_discover_condition=array('alive'=>'t','observable'=>'t');

    $hc->initiate_composit('database_name',$db_stat_discover_condition);
    foreach ($hc->get_composit('database_name') as $dn) {
	if ($hc->track()) {
	    $dn->populate_db_stat($time_id);
	}
	$sch_stat_discover_conditions=array('alive'=>'t','observable'=>'t');
	$dn->initiate_composit('schema_name',$sch_stat_discover_conditions);
	foreach ($dn->get_composit('schema_name') as $sn) {
	    $sn->set_prod_sql($dn->get_prod_sql());

	    $tbl_stat_discover_condition=array('alive'=>'t');
	    $sn->initiate_composit('table_name',$tbl_stat_discover_condition);
	    $sn->discover_tables();

	    $pro_stat_discover_condition=array('alive'=>'t');
	    $sn->initiate_composit('function_name',$pro_stat_discover_condition);
	    $sn->discover_functions();

	    if ($hc->track('counts')) {
		$sn->populate_table_stat($time_id);
	    }
	    if ($hc->track('functions')) {
		$sn->populate_function_stat($time_id);
	    }
	}
    }
}

?>
